{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# How to use Cad.py\n",
    "\n",
    "**Cad.py** is an interface used to programmatically create photonic circuits. It can be used to create any kind of segment, specifying its size, index, offset and width.\n",
    "\n",
    "To use the interface you have to import various classes:\n",
    "\n",
    "``` python\n",
    "from Cad import Circuit\n",
    "from Structs import *\n",
    "```\n",
    "\n",
    "- Circuit, which is the interface.\n",
    "- The file `Structs.py` contains classes used to define some properties of the photonic circuit\n",
    "\n",
    "## How to run the code\n",
    "\n",
    "To run the code a specific Python interpreter must be used, which is contained in this directory: `C:/Synopsys/PhotonicSolutions/2019.09/RSoft/bin/rspython.exe`\n",
    "\n",
    "## Methods \n",
    "\n",
    "### Constructor\n",
    "\n",
    "``` python\n",
    "    c1 = Circuit(dimension=d, dimension_y=n, in_file_name=\"index_file\", out_file_name=\"out_file\", , free_space_wavelength=value)\n",
    "``` \n",
    "\n",
    "Where with the `dimension` you can specify if the design is 2D or 3D (2 or 3), `dimension_y` is the height of the segments, `in_file_name` is the name of the \".ind\" file (RSoft format) without the file extension and the `out_file_name` is the name of the file with the simulation results.\n",
    "\n",
    "### Add a segment\n",
    "\n",
    "To add a segment to the circuit you can use the following method:\n",
    "\n",
    "``` python\n",
    "    c1.add_segment(pos=vec3(x, y, z), offset=vec3(x, y, z), dimension_x=n, index=i)\n",
    "```\n",
    "where `c1` is the circuit object previously created, `pos=vec3(x, y, z)` is the position of the segment, `offset=vec3(x, y, z)` is the offset of the segment, `dimension_x` is the width of the segment and the `index` is the refractive index of the segment.\n",
    "\n",
    "### Add a pathway\n",
    "\n",
    "``` python\n",
    "    c1.add_pathway(segments)\n",
    "```\n",
    "\n",
    "Where `segments` is an array of strings with the segments you want in that pathway:\n",
    "\n",
    "``` python\n",
    "    c1.add_pathway([\"1\", \"4\", \"8\"])\n",
    "```\n",
    "\n",
    "#### Add a monitor to a pathway\n",
    "\n",
    "To add a monitor to a pathway you first need to create a pathway, then you can create the monitor:\n",
    "\n",
    "``` python\n",
    "    c1.add_monitor(pathway, monitor_type.TYPE, tilt, monitor_component.COMPONENT)\n",
    "```\n",
    "\n",
    "Where `pathway` is the number of the pathway you want to add the monitor to, `monitor_type.TYPE` is the type of the monitor, `tilt` is the tilt of the monitor and `monitor_component.COMPONENT` is the component of the monitor (minor or major).\n",
    "Example:\n",
    "\n",
    "``` python\n",
    "    c1.add_monitor(1, monitor_type.MONITOR_FILE_POWER, \"0\", monitor_component.COMPONENT_MINOR)\n",
    "```\n",
    "\n",
    ">You can find all of the \"`monitor_type`\" and \"`monitor_component`\" [here.](#monitor-types-and-monitor-components)\n",
    "\n",
    "## Add a Lauch Field\n",
    "\n",
    "When you create a launch field you have to pay attention to some things:\n",
    "\n",
    "- If you don't create a launch field by calling this function there the default one will stil be created, which has position (0, 0, 0) and a power of 1.\n",
    "- When you create the first lauch field, you just change the value of the default one.\n",
    "\n",
    "To create a launch field you can use the following method:\n",
    "\n",
    "``` python\n",
    "    c1.add_launch_field(x_position, power, launch_type.TYPE, launch_pathway, launch_tilt)\n",
    "```\n",
    "\n",
    ">You can find all of the `launch_type` [here.](#launch-types)\n",
    "\n",
    "Example:\n",
    "\n",
    "``` python\n",
    "    c1.add_launch_field(0, 1, launch_type.LAUNCH_WGMODE, 0, 1)\n",
    "```\n",
    "\n",
    "## Run the Simulation\n",
    "\n",
    "Once you have added all of the components to the circuit you can run the simulation with this method:\n",
    "\n",
    "``` python\n",
    "    c1.run_simulation()\n",
    "```\n",
    "\n",
    "This method will run the simulation, create the .ind file and all of the files that contain simulation results.\n",
    "\n",
    "> **⚠️Attention!⚠️**: This method returns an array which contains the last value all the monitors recorded, which means that if you do not add a monitor it will return `None`.\n",
    "> ![None, because there are no monitors](imgs/Screenshot_4.png)\n",
    "\n",
    "### Output file directory\n",
    "\n",
    "The files will be created in a specific location. Once the simulation starts the *`/res`* directory will be generated in the folder where the .py file is. Inside of it you will find other two folders.\n",
    "\n",
    "![/res, the folder where all of the file get stored](imgs/Screenshot_1.png)\n",
    "\n",
    "#### Index file\n",
    "\n",
    "Inside the *`/res/ind`* folder there will be the .ind file.\n",
    "\n",
    "![/res/ind, the folder where the .ind file is stored](imgs/Screenshot_2.png)\n",
    "\n",
    "#### Results files\n",
    "\n",
    "Inside the *`/res/sim`* folder there will be all of the files genereted by running the simulation. The `.mon` file contains the data all the monitors captured.  If there are no monitors in the circuit that file will not be present.\n",
    "\n",
    "![/res/sim, the folder where all the file generated by the simulation are stored](imgs/Screenshot_3.png)\n",
    "\n",
    "## Special Parameters\n",
    "\n",
    "### Monitor Types and Monitor Components\n",
    "\n",
    "All the monitor types:\n",
    "\n",
    "- MONITOR_FILE_POWER\n",
    "- MONITOR_FILE_PHASE \n",
    "- MONITOR_WGMODE_POWER\n",
    "- MONITOR_WGMODE_PHASE\n",
    "- MONITOR_GAUSS_POWER \n",
    "- MONITOR_GAUSS_PHASE \n",
    "- MONITOR_LAUNCH_POWER\n",
    "- MONITOR_LAUNCH_PHASE\n",
    "- MONITOR_WG_POWER\n",
    "- MONITOR_TOTAL_POWER \n",
    "- MONITOR_FIELD_NEFF \n",
    "- MONITOR_FIELD_WIDTH \n",
    "- MONITOR_FIELD_HEIGHT\n",
    "\n",
    "All the monitor components:\n",
    "\n",
    "- COMPONENT_MINOR\n",
    "- COMPONENT_MAJOR\n",
    "\n",
    "### Launch Types\n",
    "\n",
    "All the launch types:\n",
    "\n",
    "- LAUNCH_FILE\n",
    "- LAUNCH_COMPMODE\n",
    "- LAUNCH_WGMODE\n",
    "- LAUNCH_GAUSSIAN\n",
    "- LAUNCH_RECTANGLE\n",
    "- LAUNCH_MULTIMODE\n",
    "- LAUNCH_PLANEWAVE\n",
    "  \n",
    "### An example\n",
    "\n",
    "``` python\n",
    "from Cad import Circuit\n",
    "from Structs import *\n",
    "\n",
    "def main():\n",
    "    c1 = Circuit(dimension=2, dimension_y=3, in_file_name=\"design\", out_file_name=\"result\")\n",
    "\n",
    "    c1.add_segment(pos=vec3(0, 0, 0), offset=vec3(0, 0, 1), dimension_x=1, index=2)\n",
    "    c1.add_segment(pos=vec3(2, 0, 0), offset=vec3(0, 0, 1), dimension_x=1, index=2)\n",
    "    c1.add_segment(pos=vec3(4, 0, 0), offset=vec3(0, 0, 1), dimension_x=1, index=2)\n",
    "    c1.add_segment(pos=vec3(6, 0, 0), offset=vec3(0, 0, 1), dimension_x=1, index=2)\n",
    "\n",
    "    c1.add_segment(pos=vec3(3, 0, 1), offset=vec3(0, 0, 7), dimension_x=7, index=2)\n",
    "    \n",
    "    for i in [float(k) / 2 for k in range(4, 14, 1)]:\n",
    "        for j in[float(l) / 2 for l in range(1, 12, 1)]:\n",
    "            c1.add_segment(pos=vec3(j, 0, i), offset=vec3(0, 0, 0.5), dimension_x=0.5, index=3)\n",
    "\n",
    "    c1.add_segment(pos=vec3(0, 0, 8), offset=vec3(0, 0, 1), dimension_x=1, index=2)\n",
    "    c1.add_segment(pos=vec3(2, 0, 8), offset=vec3(0, 0, 1), dimension_x=1, index=2)\n",
    "    c1.add_segment(pos=vec3(4, 0, 8), offset=vec3(0, 0, 1), dimension_x=1, index=2)\n",
    "    c1.add_segment(pos=vec3(6, 0, 8), offset=vec3(0, 0, 1), dimension_x=1, index=2)\n",
    "\n",
    "    for i in range(1,5):\n",
    "        c1.add_pathway([str(10*11+5+i)])\n",
    "    \n",
    "    \n",
    "    c1.add_lauch_field(0, 1, launch_type.LAUNCH_WGMODE, 0, 1)\n",
    "    c1.add_lauch_field(2, 1, launch_type.LAUNCH_WGMODE, 0, 1)\n",
    "    c1.add_lauch_field(4, 1, launch_type.LAUNCH_WGMODE, 0, 1)\n",
    "    c1.add_lauch_field(6, 1, launch_type.LAUNCH_WGMODE, 0, 1)\n",
    "    \n",
    "    print(c1.run_simulation())\n",
    "\n",
    "main()\n",
    "```\n",
    "\n",
    "#### Result\n",
    "\n",
    "This is the result of the code above:\n",
    "\n",
    "![The result of the simulation](imgs/Screenshot_5.png)\n"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "ccfea65839ea7b4cb7611917847ae955f8a5d3f7496a05c88f3bf628abe673bf"
  },
  "kernelspec": {
   "display_name": "Python 3.10.4 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
